package com.web.controller.system;

import com.domain.system.Role;
import com.domain.system.User;
import com.github.pagehelper.PageInfo;
import com.service.system.RoleService;
import com.service.system.UserService;
import com.web.controller.BaseController;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/system/user")
public class UserController extends BaseController {

    @Autowired
    private UserService userService;
//    @Autowired
//    private AmqpTemplate amqpTemplate;

    @Autowired
    private RoleService roleService;

    @RequestMapping("/list")
    public String findAll(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "5") Integer size){
        PageInfo pageInfo =  userService.findAll(companyId, page, size);
        request.setAttribute("page", pageInfo);
        return "system/user/user-list";
    }

    @RequestMapping("/toAdd")
    public String toAdd(){
        List<User> deptList = userService.findAll(companyId);
        request.setAttribute("deptList", deptList);
        return "system/user/user-add";
    }

    @RequestMapping("/toUpdate")
    public String toUpdate(String id){
        User user = userService.findById(id);
        List<User> deptList = userService.findAll(companyId);
        request.setAttribute("deptList", deptList);
        request.setAttribute("user", user);
        return "system/user/user-update";
    }

    @RequestMapping("/edit")
    public String toAdd(User user) throws Exception {
        user.setCompanyId(companyId);
        user.setCompanyName(companyName);
        if (StringUtils.isBlank(user.getId())) {
            Integer result = userService.save(user);

            //发送邮件通过RabbitMQ进行处理
            //在新建用户后,给用户发送邮件
            String content = "您的邮箱:" + user.getEmail() + "\n" +
                    "您的密码为:" + user.getPassword() + "\n" +
                    "登录网址为:http://localhost:8080/login.jsp";
            String subject = "欢迎进入到SaaS-Export大家庭";

            Map map = new HashMap();
            map.put("to", user.getEmail());
            map.put("subject", subject);
            map.put("content", content);

//            amqpTemplate.convertAndSend("heima113_queue_spring", map);

        }else{
            Integer result = userService.update(user);
        }
        return "redirect:/system/user/list.do";
    }

    @RequestMapping("/delete")
    public String delete(String id){
        Integer result = userService.delete(id);
        return "redirect:/system/user/list.do";
    }

    @RequestMapping("/roleList")
    public String roleList(String id){
        User user = userService.findById(id);
        request.setAttribute("user", user);
        //所有的role
        List<Role> roleList = roleService.findAll(companyId);
        request.setAttribute("roleList", roleList);
        //该角色有的本身拥有的role
        List<Role> roleUser = roleService.findRoleById(id);
        StringBuilder userRoleStr = new StringBuilder();
        for (Role role : roleUser) {
            userRoleStr.append(role.getId()).append(",");
        }
        request.setAttribute("userRoleStr", userRoleStr);
        return "system/user/user-role";
    }

    @RequestMapping("/changeRole")
    public String chaneRole(String userId, String roleIds){
        Integer result = userService.updateRole(userId, roleIds);
        return "redirect:/system/user/list.do";
    }
}
